package com.njd.meitu.tv.main;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.njd.meitu.tv.R;
import com.njd.meitu.tv.image.AbImageLoader; 
import com.njd.meitu.tv.model.MeiziTuInfo;
import com.njd.meitu.tv.view.XListView;
import com.njd.meitu.tv.widget.ScaleImageView;

public class StaggeredAdapter extends BaseAdapter {
    private Context mContext;
    private LinkedList<MeiziTuInfo> mInfos;
    private XListView mListView;
    private AbImageLoader mAbImageLoader = null;

    public StaggeredAdapter(Context context, XListView xListView) {
        mContext = context;
        mInfos = new LinkedList<MeiziTuInfo>();
        mListView = xListView;
        
        mAbImageLoader = new AbImageLoader(mContext);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        final MeiziTuInfo duitangInfo = mInfos.get(position);

        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
            convertView = layoutInflator.inflate(R.layout.infos_list, null);
            holder = new ViewHolder();
            holder.imageView = (ScaleImageView) convertView.findViewById(R.id.news_pic);
            holder.contentView = (TextView) convertView.findViewById(R.id.news_title);
            convertView.setTag(holder);
        }

        holder = (ViewHolder) convertView.getTag();
        holder.imageView.setImageWidth(duitangInfo.getWidth());
        holder.imageView.setImageHeight(duitangInfo.getHeight());
        
//        mAbImageLoader.setMaxWidth(duitangInfo.getWidth());
//        mAbImageLoader.setMaxHeight(duitangInfo.getHeight());
        
        holder.contentView.setText(duitangInfo.getMsg());
        mAbImageLoader.setEmptyImage(R.drawable.image_empty);
		mAbImageLoader.setErrorImage(R.drawable.image_error);
        mAbImageLoader.setLoadingView(convertView.findViewById(R.id.progressBar));
        mAbImageLoader.display(holder.imageView, duitangInfo.getIsrc());
        
        holder.contentView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, WebviewActivity.class);
				MeiziTuInfo info = duitangInfo;
				Bundle mBundle = new Bundle();  
				mBundle.putInt("tuId", info.getTuId());
				intent.putExtras(mBundle);
				mContext.startActivity(intent);
			}
		});
        
        
        final int imgIndex = position;
        holder.imageView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, ViewPagerActivity.class);
//				MeiziTuInfo info = duitangInfo;
				Bundle mBundle = new Bundle();  
				
//				ArrayList<String> imgList = new ArrayList<String>(); 
//				for (MeiziTuInfo mi : mInfos) {
//					imgList.add(mi.getIsrc().replace("s_", ""));
//				}
				mBundle.putSerializable("imgList", mInfos);
				mBundle.putInt("imgIndex", imgIndex);
				
				intent.putExtras(mBundle);
				mContext.startActivity(intent);
			}
		});
        
        return convertView;
    }

    class ViewHolder {
    	ScaleImageView imageView;
        TextView contentView;
        TextView timeView;
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int arg0) {
        return mInfos.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    public void addItemLast(List<MeiziTuInfo> datas) {
        mInfos.addAll(datas);
    }

    public void addItemTop(List<MeiziTuInfo> datas) {
        for (MeiziTuInfo info : datas) {
            mInfos.addFirst(info);
        }
    }
}
